package preproc;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import utils.Constants;

public class ImageReader {
	public Image readImage(String fileName) {
		try {
			BufferedImage img = ImageIO.read(new File(fileName));
			Image image = new Image();
			
			// read the original image
			image.originalImage = img;

			// compute the greyscale matrix of the image
			image.intensity = new int[img.getHeight()][img.getWidth()];
			for (int x=0; x<img.getWidth(); x++) {
				for (int y=0; y<img.getHeight(); y++) {
					int rgb = img.getRGB(x, y);
					int r	= (rgb >> 16) & 0xFF;
					int g 	= (rgb >> 8) & 0xFF;
					int b	= rgb & 0xFF;
					int val = (int)(0.3*r+0.59*g+0.11*b);
					image.intensity[y][x] = val;
				}
			}
			return image;
			
		} catch (IOException e) {
			System.err.println("Could not read " + fileName + " image:");
			if (Constants.DEBUG) {
				e.printStackTrace();
			}
			return null;
		}
	}
}
